package coop.bancocredicoop.proyectos.gd.query.documentType;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.Transaction;

import coop.bancocredicoop.proyectos.gd.domain.DocumentType;
import coop.bancocredicoop.proyectos.gd.hibernate.HibernateUtil;

public class FindDocumentTypeById {

	public DocumentType run(Long id) throws Exception {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		DocumentType result = null;

		try {
			tx = session.beginTransaction();

			result = _run(id, session);

			tx.commit();
		}
		catch (Exception e) {
			if (tx!=null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

		return result;

	}

	private DocumentType _run(Long id, Session session) throws Exception {

		Query query = session.createQuery("from DocumentType where id = :id");
		query.setLong("id", id);
		return (DocumentType) query.uniqueResult();

	}

}
